package com.dongdongshop.service.impl;

import com.dongdongshop.config.FreemarkerConfig;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.FreemarkerService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${myfreemarker.outputPath}")
    private String outputPath;

    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private ItemCatService itemCatService;

    @Override
    public void createPage(Long goodsId) {
        //第一步：创建一个 Configuration 对象，直接 new 一个对象。构造方法的参数就是 freemarker的版本号。
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        //第四步：加载一个模板，创建一个模板对象。
        FileWriter fileWriter = null;
        try {
            Template template = configuration.getTemplate("item.ftl");

            TbGoods goods = goodsService.findGoodsById(goodsId);
            TbGoodsDesc goodsDesc = goodsService.findGoodsDescById(goodsId);
            TbItemCat tbItemCat = itemCatService.selectBycategory1Id(goods.getCategory1Id());
            TbItemCat tbItemCat2 = itemCatService.selectBycategory2Id(goods.getCategory2Id());
            TbItemCat tbItemCat3 = itemCatService.selectBycategory3Id(goods.getCategory3Id());
            List<TbItem> itemlist = goodsService.selectByGoodsId(goodsId);
            //第五步：创建一个模板使用的数据集，可以是 pojo 也可以是 map。一般是 Map
            Map map = new HashMap();
            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("tbItemCat",tbItemCat);
            map.put("tbItemCat2",tbItemCat2);
            map.put("tbItemCat3",tbItemCat3);
            map.put("itemlist",itemlist);
//        第六步：创建一个 Writer (输出流)对象，一般创建一 FileWriter 对象，指定生成的文件名。
           fileWriter = new FileWriter(outputPath + goodsId +".html");
//        第七步：调用模板对象的 process 方法输出文件。
            template.process(map,fileWriter);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter != null){
                //八步：关闭流
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
